'use strict';

    angular.module('app').controller('regionupdateController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster',
            function($scope, $rootScope, $state, $timeout, toaster) {
        	if($state.includes('**.update')){
                $scope.title="修改区域信息";
                $scope.reset = false;//修改不允许重置
                var id = $state.params.id;
                activate(id);
                $scope.pageType = 'update'; 
            }else if($state.includes('**.create')){
	        	$scope.title="新增区域信息";
	        	$scope.reset = true;//新增时可以重置
	        	$scope.pageType = 'create';
            }
        	
        	validate();
			//重置表单
			$scope.resetFrom = function(){
			   $scope.record ={};
			}
        	$scope.submit= function(){
        		var _submit_url = '';
        		if($scope.pageType == 'create'){
        			_submit_url = '/dac/regionManage/region/l/create'
        		}else if($scope.pageType == 'update'){
        			_submit_url = '/dac/regionManage/region/l/update'
        		}
                var m = $scope.record;
                $scope.record.regionId = $state.params.id;
                $scope.record.status = '1';
                if(m){
                    $scope.isDisabled = true;
                    $.ajax({
        				type: 'POST',
        	            dataType: 'json',
        				contentType:'application/json;charset=UTF-8',
        				url : _submit_url,
        				data: angular.toJson($scope.record)
        			}).then(function(result){
                        if(result.httpCode ==200){//成功
                            toaster.clear('*');
                            toaster.pop('success', '', "保存成功");
                            $timeout(function(){
                                $state.go('main.regionManage.region.list');
                            },2000);
                        }else{
                            toaster.clear('*');
                            toaster.pop('error', '', result.msg);
                            $scope.isDisabled = false;
                        }
                    });
                }
               
            }
        	
        	// 初始化页面
            function activate(id) {
    	        $scope.loading = true;
            	$.ajax({
            		type: 'PUT',
    	            dataType: 'json',
    				contentType:'application/json;charset=UTF-8',
    				url : '/dac/regionManage/region/l/detail',
    				data: angular.toJson({'regionid': id})
    			}).then(function(result) {
    		        $scope.loading = false;
    				if (result.httpCode == 200) {
    					$scope.record = result.data;
    				} else {
    					$scope.msg = result.msg;
    				}
    				$scope.$apply();
    			});
            }
        	
        	function validate(){
                jQuery('form').validate({
					rules: {
                		regionName: {
                    		required: true
                    	},
                        regionType: {
                            required: true,
                            chineseCheck: true,
                            maxLengthB: [2]
                        },
                    	status: {
                    		required: true
                    	}
                    },
                    messages: {
                    	regionName: {
                    		required: '请输入区域名称'
                    	},
                        regionType: {
                            required: '请输入区域类型',
                            chineseCheck: '只能输入中文',
                            maxLengthB: '区域类型长度不能大于1位'
                        },
                        status: {
                		    required: '请选择状态'
                	    }
                    },
                    submitHandler: function() {
                        $scope.submit();
                    }
                });
            }

    }]);